﻿Public Class Validations


    'Public Sub Habilitar(ByVal form As Form, ByVal aux As Boolean)
    '    For Each element As GroupBox In form.Controls
    '        For Each element2 As Control In element.Controls
    '            element2.Enabled = True
    '        Next element2
    '    Next element
    'End Sub

    Public Function resetTextBox(ByVal form As Form) As Boolean
        For Each element As Control In form.Controls
            If TypeOf (element) Is TextBox Then
                element.Text = ""
            End If
        Next element
        Return True
    End Function

    Public Function resetComboBox(ByVal form As Form) As Boolean
        For Each element As ComboBox In form.Controls
            If element.Enabled() = True Then
                element.SelectedIndex = -1
            End If
        Next element
        Return True
    End Function

    Public Function resetCheckBox(ByVal form As Form) As Boolean
        For Each element As CheckBox In form.Controls
            If element.Enabled() = True Then
                element.Checked = False
            End If
        Next element
        Return True
    End Function

    Public Function resetRdButton(ByVal form As Form) As Boolean
        For Each element As RadioButton In form.Controls
            If element.Enabled() = True Then
                element.Checked = False
            End If
        Next element
        Return True
    End Function

    Public Function validarNoNull(ByVal form As Form) As Boolean
        For Each element As Control In form.Controls
            If element.GetType() Is GetType(TextBox) And element.Enabled() = True Then
                If element.Text = "" Then
                    MessageBox.Show("Algunos campos estan vacíos")
                    SetTheRegion(element, 2, 2, 4)
                    element.Focus()
                    Return False
                End If
            End If
        Next element
        Return True
    End Function

    Public Function validaComboBox(ByVal form As Form) As Boolean
        For Each element As ComboBox In form.Controls
            If element.Enabled() = True Then
                If element.SelectedIndex = -1 Then
                    MessageBox.Show("Falta seleccionar algun dato")
                    SetTheRegion(element, 2, 2, 4)
                    element.Focus()
                    Return False
                End If
            End If
        Next element
        Return True
    End Function

    Public Function validaDatePickers(ByVal form As Form) As Boolean
        For Each element As DateTimePicker In form.Controls
            If element.Enabled() = True Then
                If ((Date.Parse(element.Text) < Date.Parse("01/01/1900")) Or (Date.Parse(element.Text) > Date.Parse(Date.Now))) Then
                    SetTheRegion(element, 2, 2, 4)
                    MessageBox.Show("La fecha ingresada es incorrecta")
                    element.Focus()
                    Return False
                End If
            End If
        Next element
        Return True
    End Function

    Public Function validaRdButton(ByVal form As Form) As Boolean
        For Each element As RadioButton In form.Controls
            If element.Enabled = True Then
                If element.Checked = False Then
                    MessageBox.Show("Falta seleccionar un dato")
                    element.Focus()
                    Return False
                End If
            End If
        Next element
        Return True
    End Function


    Public Sub DrawTextBoxRectangles(ByVal form As Form, ByVal e As System.Windows.Forms.DrawItemEventArgs)
        Dim p As New Pen(Color.Red, 2)
        For Each element As TextBox In form.Controls
            e.Graphics.DrawRectangle(p, New Rectangle(element.Location + New Size(1, 1), element.Size - New Size(2, 2)))
        Next element
        p.Dispose()
    End Sub

    Public Sub DrawCmbBoxRectangles(ByVal form As Form, ByVal e As System.Windows.Forms.DrawItemEventArgs)
        Dim p As New Pen(Color.Red, 2)
        For Each element As ComboBox In form.Controls
            e.Graphics.DrawRectangle(p, New Rectangle(element.Location + New Size(1, 1), element.Size - New Size(2, 2)))
        Next element
        p.Dispose()
    End Sub

    Public Sub DrawDatePickersRectangles(ByVal form As Form, ByVal e As System.Windows.Forms.DrawItemEventArgs)
        Dim p As New Pen(Color.Red, 2)
        For Each element As DateTimePicker In form.Controls
            e.Graphics.DrawRectangle(p, New Rectangle(element.Location + New Size(1, 1), element.Size - New Size(2, 2)))
        Next element
        p.Dispose()
    End Sub


    Public Sub RellenoTextBoxRectangles(ByVal form As Form)
        For Each element As TextBox In form.Controls
            SetTheRegion(element, 0, 0, -4)
        Next element
    End Sub

    Public Sub RellenoCmbBoxRectangles(ByVal form As Form)
        For Each element As ComboBox In form.Controls
            SetTheRegion(element, 0, 0, -4)
        Next element
    End Sub

    Public Sub RellenoDatePickerRectangles(ByVal form As Form)
        For Each element As DateTimePicker In form.Controls
            SetTheRegion(element, 0, 0, -4)
        Next element
    End Sub

    '

    Private Sub SetTheRegion(ByVal Objeto As Object, ByVal X As Double, ByVal Y As Double, ByVal Borde As Double)
        Objeto.Region = New Region(New Rectangle(X, Y, Objeto.Width - Borde, Objeto.Height - Borde))
    End Sub





End Class
